package com.example.spider.provider.m3u8.dto;

import com.example.spider.provider.m3u8.M3u8Parser;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

public enum M3u8SpecEnum {

    /**
     * 加密密钥
     */
    EXT_X_KEY("EXT-X-KEY", (m, c) -> {
        //如果含有此字段，则获取加密算法以及获取密钥的链接
        M3u8Key m3u8Key = M3u8Parser.parseKey(c);
        Map<String, M3u8Key> keyMap = new HashMap<>();
        keyMap.put(m3u8Key.getUri(), m3u8Key);
        m.setKeyMap(keyMap);
    }),


    EXTM3U_MARK("EXTM3U", (m, c) -> {
    }),

    EXT_X_SESSION_KEY("EXT-X-SESSION-KEY", (m, c) -> m.setSessionKey(null)),
    EXT_X_ALLOW_CACHE("EXT-X-ALLOW-CACHE", (m, c) -> {

    }),

    //EXT_X_STREAM_INF("EXT-X-STREAM-INF", (m, c) -> m.setExtStreamInf(null)),

    version("EXT-X-VERSION", M3u8MetaInfo::setVersion),

    TARGET_DURATION("EXT-X-TARGETDURATION", M3u8MetaInfo::setDefaultDuration),

    independentSegments("EXT-X-INDEPENDENT-SEGMENTS", (m, c) -> m.setIndependentSegments(true)),

    MEDIA_SEQUENCE("EXT-X-MEDIA-SEQUENCE", (m, c) -> m.setMediaSequence(Integer.parseInt(c))),

    // 继续指令
    DISCONTINUITY("EXT-X-DISCONTINUITY", (m, c) -> {
    }),

    // 结束
    ENDLIST("EXT-X-ENDLIST", (m, c) -> {
    }),

    /**
     * EVENT / VOD 不解析
     */
    PLAYLIST_TYPE("EXT-X-PLAYLIST-TYPE", M3u8MetaInfo::setType),

    /**
     * 扩展定义，不解析
     */
    start("EXT-X-START", (m, c) -> m.setStart(null)),

    /**
     * 扩展定义，不解析
     */
    define("EXT-X-DEFINE", (m, c) -> m.setDefine(null)),

    /**
     * 扩展定义，不解析
     */
    media("EXT-X-MEDIA", (m, c) -> {
    }),

    /**
     * 扩展定义，不支持解析
     */
    STREAM_INF("EXT-X-STREAM-INF", (m, c) -> m.setStreamInf(null)),

    /**
     * 扩展定义，不解析
     */
    I_FRAME_STREAM_INF("EXT-X-I-FRAME-STREAM-INF", (m, c) -> m.setIframeStreamInf(null)),

    /**
     * 扩展定义，不解析
     */
    EXT_X_SESSION_DATA("EXT-X-SESSION-DATA", (m, c) -> m.setSessionData(null)),


    ;


    private final String key;

    private final BiConsumer<M3u8MetaInfo, String> valueSetter;

    M3u8SpecEnum(String key, BiConsumer<M3u8MetaInfo, String> valueSetter) {
        this.key = key;
        this.valueSetter = valueSetter;
    }

    public static M3u8SpecEnum findByKey(String key) {
        for (M3u8SpecEnum value : values()) {
            if (value.key.equals(key)) {
                return value;
            }
        }
        return null;
    }

    public String getKey() {
        return key;
    }

    public BiConsumer<M3u8MetaInfo, String> getValueSetter() {
        return valueSetter;
    }

}
